/*
 * 去除文件中的重复行
 * **/
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

int main(int argc, char** argv) {

    if(argc < 2)
    {
        std::cout << "Please input filename" << std::endl;
    }

    std::string filename(argv[1]);

    std::ifstream ifs(filename, std::ios::in);
    std::ofstream backOfs(filename+".back", std::ios::out);

    backOfs.flush();
    std::string str;
    std::vector<std::string> lines;
    while (std::getline(ifs, str))
    {
        backOfs << str << std::endl;
        if(lines.empty() || std::find(lines.begin(), lines.end(), str) == lines.end())
        {
            lines.push_back(str);
        }
    }

    ifs.close();
    backOfs.close();


    std::ofstream ofs(filename, std::ios::out);
    ofs.flush();
    for (std::string singleLine : lines) {
        ofs << singleLine << std::endl;
    }
    ofs.close();

    return 0;
}
